package controller

import (
	"github.com/gin-gonic/gin"
	"shop-bean/constant"
	"shop-bean/model"
	"shop-bean/request"
	"shop-common/library/serializer"
	"shop-common/utils"
	"shop-common/utils/contextArgs"
	"shop-common/utils/pagehelper"
	"shop-common/utils/queryWrapper"
	"shop-service/business"
)

type IndexImgController struct {
}

// @Tags 轮播图片
// @Summary 分页
// @Security ApiKeyAuth
// @function: Page
// @Accept  json
// @Produce  json
// @Param current query string false "当前页，默认1"
// @Param size query string false "每页大小，默认10"
// @Param status query int false "状态  1:无效  2:正常"
// @Success 200 {object} string "{"code":1," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /admin/indexImg/page [get]
func (c *IndexImgController) Page(ctx *gin.Context) {
	form := pagehelper.SearchPageRequest{}
	if err := serializer.Verify(ctx, &form); err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	paginator := pagehelper.New().SetOffset(&form).SetFilter([]string{"img_id"}).IsPush(form.Status > 0, queryWrapper.Eq("status", form.Status)).Paginator()
	page, err := business.ServiceImpl().IndexImgService.FindPage(ctx, paginator)
	if err != nil {
		serializer.Fail(ctx, serializer.PublicQueryErr)
		return
	}

	serializer.SuccessData(ctx, "", page)
}

// @Tags 轮播图片
// @Summary 保存
// @Security ApiKeyAuth
// @function: Save
// @Accept  json
// @Produce  json
// @Param data body request.IndexImgRequest true "请求参数"
// @Success 200 {object} string "{"code":1," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /admin/indexImg [post]
func (c *IndexImgController) Save(ctx *gin.Context) {
	form := request.IndexImgRequest{}
	if err := serializer.Verify(ctx, &form); err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	entity := &model.IndexImg{}
	err := utils.StructMapStrict(form, entity)
	if err != nil {
		serializer.Fail(ctx, serializer.PublicCreateErr)
		return
	}
	entity.ShopId = constant.SHOP_ID

	err = business.ServiceImpl().IndexImgService.Create(ctx, entity)
	if err != nil {
		serializer.Fail(ctx, serializer.PublicCreateErr)
		return
	}
	serializer.Success(ctx, "")
}

// @Tags 轮播图片
// @Summary 获取信息
// @Security ApiKeyAuth
// @function: Info
// @Param imgId path string true "主键"
// @Success 200 {object} string "{"code":1," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /admin/indexImg/info/{imgId} [get]
func (c *IndexImgController) Info(ctx *gin.Context) {
	imgId, err := contextArgs.GetUriIntPath(ctx, "imgId")
	if err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	shopId, err := contextArgs.GetSHopId(ctx)
	if err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	wp := queryWrapper.Eq("img_id", imgId).And(queryWrapper.Eq("shop_id", shopId))
	indexImg, err := business.ServiceImpl().IndexImgService.FindOne(ctx, wp)
	if err != nil {
		serializer.Fail(ctx, serializer.PublicQueryErr)
		return
	}

	serializer.SuccessData(ctx, "", indexImg)
}

// @Tags 轮播图片
// @Summary 修改
// @Security ApiKeyAuth
// @function: Update
// @Accept  json
// @Produce  json
// @Param data body request.IndexImgRequest true "请求参数"
// @Success 200 {object} string "{"code":1," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /admin/indexImg [put]
func (c *IndexImgController) Update(ctx *gin.Context) {
	form := request.IndexImgRequest{}
	if err := serializer.Verify(ctx, &form); err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	if form.ImgId < 1 {
		serializer.Fail(ctx, serializer.IdNotEmpty)
		return
	}

	info, err := business.ServiceImpl().IndexImgService.FindById(ctx, form.ImgId)
	if err != nil {
		serializer.Fail(ctx, serializer.PublicUpdateErr)
		return
	}

	var oldPic string
	if info != nil {
		oldPic = info.Pic
	}

	err = business.ServiceImpl().IndexImgService.Updates(ctx, &model.IndexImg{
		ImgId:    form.ImgId,
		ShopId:   constant.SHOP_ID,
		Pic:      form.Pic,
		FileId:   form.FileId,
		Des:      form.Des,
		Title:    form.Title,
		Link:     form.Link,
		Status:   form.Status,
		Seq:      form.Seq,
		Type:     form.Type,
		Relation: form.Relation,
		ProdName: form.ProdName,
	}, oldPic)
	if err != nil {
		serializer.Fail(ctx, serializer.PublicUpdateErr)
		return
	}

	serializer.Success(ctx, "")
}

// @Tags 轮播图片
// @Summary 删除
// @Security ApiKeyAuth
// @function: Delete
// @Accept  json
// @Produce  json
// @Param data body request.CommonBatchRequest true "请求参数"
// @Success 200 {object} string "{"code":0," result":{}, "message":"操作成功", "time":1618493448}"
// @Router /admin/indexImg [delete]
func (c *IndexImgController) Delete(ctx *gin.Context) {
	form := request.CommonBatchRequest{}
	if err := serializer.Verify(ctx, &form); err != nil {
		serializer.Fail(ctx, err.Error())
		return
	}

	err := business.ServiceImpl().IndexImgService.DeleteBatch(ctx, form.Ids)
	if err != nil {
		serializer.Fail(ctx, serializer.PublicDeleteErr)
		return
	}

	serializer.Success(ctx, "")
}
